package com.zhu.androidsample.business.phonelocation;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhu.androidsample.R;
import com.zhu.androidsample.business.phonelocation.bean.PhoneLocationBean;
import com.zhu.androidsample.business.phonelocation.presenter.PhoneLocationPresenter;
import com.zhu.androidsample.business.phonelocation.view.PhoneLocationView;
import com.zhu.androidsample.common.BaseActivity;

public class PhoneLocationActivity extends BaseActivity implements View.OnClickListener {

    ////////////////////////////////约定区////////////////////////////////////////
    /** 更新页面的号码归属地信息 **/
    public static final int UPDATE_LOCATION = 0x010009;

    ////////////////////////////////view区////////////////////////////////////////
    EditText phone_number;
    PhoneLocationView phoneLocationView;
    Button submit;

    PhoneLocationPresenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_location);
        phone_number = (EditText) findViewById(R.id.pl_phone_number);
        submit = (Button) findViewById(R.id.button);
        phoneLocationView = (PhoneLocationView) findViewById(R.id.pl_phone_location);

        init();
    }

    private void init() {
        submit.setOnClickListener(this);
        presenter = new PhoneLocationPresenter(this);
    }

    @Override
    public void onClick(View v) {
        presenter.searchPhoneLocation(phone_number.getText().toString());

    }


    ////////////////////////////////响应区////////////////////////////////////////

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case UPDATE_LOCATION:
                updateLocation((PhoneLocationBean) msg.obj);
                break;
        }
        return super.handleMessage(msg);
    }

    /**
     * 请求数据后刷新试图方法
     * 功能：设置号码归属地查询结果
     * @param bean 接收到的模型参数
     */
    private void updateLocation(PhoneLocationBean bean) {
        phoneLocationView.setBean(bean);
    }
}
